package com.yanze.schoolinformation.utils;

import android.content.Context;

import com.yanze.schoolinformation.app.C;
import com.yanze.schoolinformation.model.bean.resp.RespImage;
import com.yanze.schoolinformation.model.http.DeforObserver;
import com.yanze.schoolinformation.model.http.reqo.DataReqo;

import java.io.File;
import java.util.ArrayList;

import io.reactivex.disposables.Disposable;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;

/**
 * 单个图片上传工具类
 * Created by MrZheng on 2018/5/9.
 */

public class UpFile {

    ArrayList<String> waitImages;//待上传图片
    ArrayList<String> successImages;//上传成功图片
    Context mContext;
    UpStateListener mUpstateListener;

    int mCurrindex = 0;

    public UpFile(Context context, ArrayList<String> waitImages) {
        this.waitImages = waitImages;
        this.mContext = context;
        successImages = new ArrayList<>();
    }

    public void start(UpStateListener upStateListener) {
        this.mUpstateListener = upStateListener;
        if (waitImages == null || waitImages.isEmpty()) {
            return;
        }
        up();
    }

    public void resert() {
        up();
    }

    /**
     * 请求网络
     */
    private void up() {
        mUpstateListener.onProgrees(mCurrindex + 1);

        File file = new File(waitImages.get(mCurrindex));
        MediaType IMAGE_TYPE = MediaType.parse("image/jpeg");
        RequestBody requestBody = RequestBody.create(IMAGE_TYPE, file);
        MultipartBody.Part body = MultipartBody.Part.createFormData("file", file.getName(), requestBody);
        DataReqo.getInstant().upImage(C.url.API_School, body)
                .subscribe(new DeforObserver<RespImage>() {
                    @Override
                    public void _onSubscribe(Disposable disposable) {
                    }

                    @Override
                    public void _onNext(RespImage entity) {
                        if (entity.getStatus() == 200) {
                            successImages.add(entity.getData());
                            mCurrindex++;
                            if (mCurrindex < waitImages.size()) {
                                up();
                            }else{
                                mUpstateListener.onSucceed(successImages);
                            }
                        } else {
                            mUpstateListener.onFailed(mCurrindex + 1,entity.getMsg());
                        }
                    }

                    @Override
                    public void _onError(String msg) {
                        mUpstateListener.onFailed(mCurrindex + 1,msg);
                    }
                });
    }


    /**
     * 图片上传回电接口
     * 上传成功, 通知外界
     * 上传失败,是否重试
     */
    public interface UpStateListener {
        //上传成功
        void onSucceed(ArrayList<String> successImage);

        //上传进度
        void onProgrees(int currnetIndex);

        //上传失败
        void onFailed(int currnetIndex,String msg);
    }
}
